<template>
    <div class="articles">
        <banner></banner>
        <div class="site-content animate">
            <!-- 文章目录 -->
            <!-- <div id="article-menus">
                <menu-tree :menus="menus" child-label="child"></menu-tree>
            </div> -->
            <main class="site-main">
                <article class="hentry">
                    <!-- 文章头部 -->
                    <header class="entry-header">
                        <!-- 标题输出 -->
                        <h1 class="entry-title">{{ article.title }}</h1>
                        <hr>
                        <div class="breadcrumbs">
                            <div id="crumbs">最后更新时间：{{ article.updateTime }}</div>
                        </div>
                    </header>
                    <!-- 正文输出 -->
					<div class="entry-content" v-highlight>
						<!-- {{文章数据}} -->
						<p v-html="article.context"></p>
                    </div>
                    <!-- 文章底部 -->
                    <section-title>
                        <footer class="post-footer">
                            <!-- 阅读次数 -->
                            <div class="post-like">
                                <i class="iconfont iconeyes"></i>
                                <span class="count">{{ article.reading }}</span>
                            </div>
							<!-- <div class="post-like">
							    <i class="iconfont message"></i>
							    <span class="count">点击评论</span>
							</div> -->
                            <div class="donate" @click="like()">
                                <span v-if="article.like != true">赞 {{ article.likeNum }}</span>
								<span v-if="article.like == true">已点赞 {{ article.likeNum }}</span>
                            </div>
                            <!-- 文章标签 -->
                            <div class="donate" @click="cart()" style="margin-left: 10px;">
                                <span v-if="article.cart != true">收藏 {{ article.cartNum }}</span>
								<span v-if="article.cart == true">已收藏 {{ article.cartNum }}</span>
                            </div>
                        </footer>
                    </section-title>
					<el-button @click="insertVO=true" style="color: skyblue;">点击评论</el-button>
					
					<!-- 审核弹窗 -->
					<el-dialog title="添加评论" :visible.sync="insertVO" width="500px" append-to-body>
					  </el-form>
					  <el-input v-model="insertComment"></el-input>
					  <div slot="footer" class="dialog-footer">
					    <el-button type="primary" @click="come()">发布</el-button>
					    <el-button @click="unCome()">取消</el-button>
					  </div>
					</el-dialog>
					
                    <!--评论-->
                    <div class="comments">
                        <comment v-for="item in comments" :key="item.id" :comment="item">
							<!-- 回复评论 -->
                            <!-- <template v-if="item.length">
                                <comment v-for="reply in item.reply" :key="reply.id" :comment="reply"></comment>
                            </template> -->
							<el-button v-if="item.userStatus == 1" @click="deleted(item.id)">删除</el-button>
                        </comment>
                    </div>
					<!--加载更多-->
					<div class="more" v-show="hasNextPage">
					    <div class="more-btn" @click="loadMore">More</div>
					</div>
                </article>
            </main>
        </div>
    </div>
</template>

<script>
    import Banner from '@/components/banner'
    import sectionTitle from '@/components/section-title'
    import comment from '@/components/comment'
    import menuTree from '@/components/menu-tree'
    import {fetchComment} from '../api'
	import { getArticleInfoById, like, unLike,
				cart, unCart } from '@/api/article'
	import { getCommentByArticle, insertComment, deletedComment } from '@/api/comment'
    export default {
        name: 'articles',
        data(){
          return{
			  articleId: '',
              showDonate: false,
			  // like: false,
			  // cart: true,
              comments: [],
              menus: [],
			  article: [],
			  currPage: 1,
			  hasNextPage: true,
			  insertVO: false,
			  insertComment: ''
          }
        },
        components: {
            Banner,
            sectionTitle,
            comment,
            menuTree
        },
        methods: {
         async getComment(){
			  let data = await getCommentByArticle({
				articleId: this.articleId,
			  	pageNum: this.currPage,
			  	pagesize: 5
			  })
			  this.comments = data.articleCommentList
          },
		  async getArticleInfo() {
			let data = await getArticleInfoById(this.articleId)
			this.article = data.articles
		  },
		  async loadMore() {
		  	this.currPage = this.currPage + 1
			let data = await getCommentByArticle({
				articleId: this.articleId,
				pageNum: this.currPage,
				pagesize: 5
			})
			this.comments = this.comments.concat(data.articleCommentList || [])
		  },
		  async come() {
			  if (this.insertComment == '') {
				  alert("评论信息不能为空")
				  return
			  }
			  await insertComment({
				  articleId: this.articleId,
				  comment: this.insertComment
			  })
			 this.getComment()
			 this.insertComment = ''
			 this.insertVO = false
		  },
		  unCome() {
			  this.insertComment = ''
			  this.insertVO = false
		  },
		  async like() {
			  if (this.article.like == true) {
				  let data = await unLike(this.articleId)
				  alert(data.msg)
				  this.getArticleInfo()
				  return
			  }
			   let data = await like(this.articleId)
			   alert(data.msg)
			   this.getArticleInfo()
			   
		  },
		  async cart() {
			  if (this.article.cart == true) {
				  let data = await unCart(this.articleId)
				  alert(data.msg)
				  this.getArticleInfo()
				  return
			  }
			  let data = await cart(this.articleId)
			  alert(data.msg)
			  this.getArticleInfo()
		  },
			async deleted(id) {
				let data = await deletedComment(id)
				alert(data.msg)
				this.getComment()
			}
        },
        mounted(){
			this.getArticleInfo()
        },
        created() {
			this.articleId = this.$route.params.id
            this.getComment()
        }
    }
</script>
<style scoped lang="less">
    .site-content {
        position: relative;
        .site-main {
            padding: 80px 0 0 0;
        }
    }
    #article-menus{
        position: sticky;
        top: 0;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .1);
        border-radius: 3px;
        padding: 15px;
        width: 300px;
        transform: translateX(-120%) translateY(150px);
        font-size: 14px;
    }
    article.hentry {
        .entry-header {
            .entry-title {
                font-size: 23px;
                font-weight: 600;
                color: #737373;
                margin: 0.67em 0;

                &:before {
                    content: "#";
                    margin-right: 6px;
                    color: #d82e16;
                    font-size: 20px;
                    font-weight: 600;
                }
            }

            hr {
                height: 1px;
                border: 0;
                background: #EFEFEF;
                margin: 15px 0;
            }

            .breadcrumbs {
                font-size: 14px;
                color: #D2D2D2;
                text-decoration: none;
                margin-bottom: 30px;
            }
        }

        .entry-content {}

        footer.post-footer {
            width: 100%;
            padding: 20px 10px;
            margin-top: 30px;
            height: 65px;
            position: relative;
            i{
                font-size: 18px;
                margin-right: 5px;
            }
            .post-like {
                float: right;
                margin: 7px 0 0 20px;
            }

            .post-share {
                float: right;
                list-style: none;
                margin-right: 20px;
            }

            .donate {
                float: left;
                line-height: 36px;
                border-radius: 100%;
                -webkit-border-radius: 100%;
                -moz-border-radius: 100%;
                border: 1px solid #2B2B2B;
                &:hover{
                    border: 1px solid goldenrod;
                    span{
                        color: goldenrod;
                    }
                }
                span {
                    color: #2B2B2B;
                    padding: 10px;
                    position: relative;
                    cursor: pointer;
                }

                .donate_inner {
                    display: none;
                    margin: 0;
                    list-style: none;
                    position: absolute;
                    left: 80px;
                    top: -40px;
                    background: #FFF;
                    padding: 10px;
                    border: 1px solid #ddd;
                    box-shadow: 0 2px 6px rgba(0, 0, 0, .08);
                    border-radius: 3px;
                    &.show{
                        display: block;
                    }
                    li {
                        float: left;
                    }

                    img {
                        width: 100px;
                    }
                    p {
                        text-align: center;
                        font-size: 15px;
                        color: #D2D2D2;
                        line-height: 1rem;
                    }
                }

                .donate_inner:after, .donate_inner:before {
                    content: "";
                    position: absolute;
                    left: 0;
                    bottom: 45%;
                    margin-left: -8px;
                    border-top: 8px solid transparent;
                    border-bottom: 8px solid transparent;
                    border-right: 8px solid #fff;
                }

                .donate_inner:before {
                    left: -1px;
                    border-right: 8px solid #ddd;
                }

            }

            .post-tags {
                margin: 7px 0 0 20px;
                float: left;
                text-transform: uppercase;
                a:hover{
                    color: #ff6d6d;
                }
            }
        }
        .open-message {
            margin: 50px 0;
            position: relative;
            background: #2B2B2B;
            padding: 10px 30px;
            border-radius: 3px;
            font-size: 14px;
            color: #fff;
            &:after {
                content: "";
                border-left: 10px solid transparent;
                border-right: 10px solid transparent;
                border-bottom: 10px solid #2B2B2B;
                position: absolute;
                top: -8px;
                left: 48%;
            }
            p {
                margin: 10px 0;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            a {
                color: #A0DAD0;
                padding: 0 5px;
            }
        }
    }
	
	.more{
	    margin: 50px 0;
	    .more-btn{
	        width: 100px;
	        height: 40px;
	        line-height: 40px;
	        text-align: center;
	        color: #ADADAD;
	        border: 1px solid #ADADAD;
	        border-radius: 20px;
	        margin: 0 auto;
	        cursor: pointer;
	        &:hover{
	            color: #8fd0cc;
	            border: 1px dashed #8fd0cc;
	        }
	    }
	}
	
    @media (max-width: 1490px) {
        #article-menus {
            display: none;
        }
    }
    @media (max-width: 800px) {
        #article-menus {
            display: none;
        }
    }
</style>
